package com.example.service;

import com.example.elasticsearch.data.MyDocument;
import org.elasticsearch.index.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyDocumentSearchService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    // match 查询
    public List<MyDocument> searchByMatchQuery(String text) {
        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("textField", text);
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery).build();
        return executeSearch(searchQuery);
    }

    // match_all 查询
    public List<MyDocument> searchByMatchAllQuery() {
        MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery).build();
        return executeSearch(searchQuery);
    }

    // term 查询
    public List<MyDocument> searchByTermQuery(String keyword) {
        TermQueryBuilder termQuery = QueryBuilders.termQuery("keywordField", keyword);
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery).build();
        return executeSearch(searchQuery);
    }

    // range 查询
    public List<MyDocument> searchByRangeQuery(int start, int end) {
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("integerField").gte(start).lte(end);
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(rangeQuery).build();
        return executeSearch(searchQuery);
    }

    // bool 查询
    public List<MyDocument> searchByBoolQuery(String text, boolean booleanValue) {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("textField", text))
                .filter(QueryBuilders.termQuery("booleanField", booleanValue));
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQuery).build();
        return executeSearch(searchQuery);
    }

    // wildcard 查询
    public List<MyDocument> searchByWildcardQuery(String wildcardPattern) {
        WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery("textField", wildcardPattern);
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(wildcardQuery).build();
        return executeSearch(searchQuery);
    }

    // prefix 查询
    public List<MyDocument> searchByPrefixQuery(String prefix) {
        PrefixQueryBuilder prefixQuery = QueryBuilders.prefixQuery("keywordField", prefix);
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(prefixQuery).build();
        return executeSearch(searchQuery);
    }

    // exists 查询
    public List<MyDocument> searchByExistsQuery(String fieldName) {
        ExistsQueryBuilder existsQuery = QueryBuilders.existsQuery(fieldName);
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(existsQuery).build();
        return executeSearch(searchQuery);
    }

    // 执行查询并返回结果
    private List<MyDocument> executeSearch(NativeSearchQuery searchQuery) {
        SearchHits<MyDocument> searchHits = elasticsearchRestTemplate.search(searchQuery, MyDocument.class);
        return searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
    }
}
